<template>
  <view class="share-section">
    <view
      class="share-content"
      :style="{ paddingTop: 'calc(' + statusBarHeight + 'px + 88rpx);' }"
    >
      <view class="close-section" @click="closeShare">
        <i class="iconfont icon-arrow-down"></i>
        <view>{{ $t("common.close") }}</view>
      </view>
      <view class="to-friend">推荐给朋友</view>
      <view class="poster-img">
        <image
          class="share-img"
          v-if="imgshow"
          :src="img_src"
          mode="aspectFill"
          @click="previewImg"
        />

        <view class="download-box" v-if="imgshow" @click="saveEwm">
          <i class="iconfont icon-download"></i>
          <button
            v-if="!openSettingBtnHidden"
            class="open-btn"
            hover-class="none"
            open-type="openSetting"
            @opensetting="handleSetting"
          >
            {{ $t("common.album") }}
          </button>
        </view>
      </view>

      <view class="poster-down-text">选择分享方式，分享给好友</view>
      <view class="share-btn-group">
        <view
          class="share-btn-item"
          v-for="(item, index) in shareList"
          :key="index"
          @click="handleToShare(item)"
        >
          <view class="item-icon-box">
            <i
              v-if="item.type === 'download'"
              class="iconfont"
              :style="{ color: item.color }"
              :class="item.icon"
            ></i>
            <template v-if="asyncImgs">
              <image
                class="img"
                v-if="item.type === 'product_taglink'"
                :src="asyncImgs.video_shop['icon-key']"
                mode="widthFix"
              ></image>
              <image
                class="img"
                v-if="item.type === 'product_h5url'"
                :src="asyncImgs.video_shop['icon-link']"
                mode="widthFix"
              ></image>
            </template>
          </view>
          <view class="item-text">{{ item.text }}</view>
        </view>
      </view>
      <view class="safe"></view>
    </view>
    <view class="share-bg" @click="closeShare">
      <view class="grey-bg"></view>
      <view class="white-bg"></view>
    </view>
  </view>
</template>

<script>
import { sharerLink } from "@/api/videoShop";

export default {
  name: "",
  props: {
    product_id: {
      type: String,
      default: ""
    },
    sharer_id: {
      type: [String, Number],
      default: ""
    }
  },
  data() {
    return {
      shareList: [
        {
          icon: "icon-download",
          text: "保存海报",
          type: "download",
          color: "#000"
        },
        {
          text: "复制口令",
          type: "product_taglink",
          color: "#000"
        },
        {
          text: "复制连接",
          type: "product_h5url",
          color: "#000"
        }
      ],

      show: false,
      poster_show: false,
      imgshow: false,
      img_src: "",

      product_taglink: "",
      product_h5url: "",
      openSettingBtnHidden: true //是否授权
    };
  },
  mounted() {
    this.generate();
  },
  watch: {},
  computed: {},
  methods: {
    closeShare() {
      this.$emit("closeShare");
    },
    handleToShare(item) {
      switch (item.type) {
        case "download":
          this.saveEwm();
          break;

        default:
          uni.setClipboardData({
            data: this[item.type]
          });
          break;
      }
    },

    //微信小程序保存到相册
    handleSetting(e) {
      if (!e.detail.authSetting["scope.writePhotosAlbum"]) {
        this.openSettingBtnHidden = false;
      } else {
        this.openSettingBtnHidden = true;
      }
    },
    //获取授权
    saveEwm() {
      var _self = this;
      // #ifdef MP
      //获取相册授权
      uni.getSetting({
        success(res) {
          if (!res.authSetting["scope.writePhotosAlbum"]) {
            uni.authorize({
              scope: "scope.writePhotosAlbum",
              success() {
                //这里是用户同意授权后的回调
                _self.saveImgToLocal();
              },
              fail() {
                //这里是用户拒绝授权后的回调
                _self.openSettingBtnHidden = false;
              }
            });
          } else {
            //用户已经授权过了
            _self.saveImgToLocal();
          }
        }
      });
      // #endif
      // #ifdef APP-PLUS
      _self.saveImgToLocal();
      // #endif
      // #ifdef WEB
      this.$utils.saveImgWeb(this.img_src);
      // #endif
    },
    //下载并保存图片
    saveImgToLocal() {
      uni.showModal({
        title: this.$t("order.prompt"),
        content: this.$t("common.shareContent"),
        success: (res) => {
          if (res.confirm) {
            uni.downloadFile({
              url: this.img_src, //图片地址
              success: (res) => {
                if (res.statusCode === 200) {
                  uni.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: () => {
                      uni.showToast({
                        title: this.$t("common.saveSuc"),
                        icon: "none"
                      });
                    },
                    fail: () => {
                      uni.showToast({
                        title: this.$t("common.saveFail"),
                        icon: "none"
                      });
                    }
                  });
                }
              },
              fail: () => {
                uni.showToast({
                  title: "保存失败，请检查是否开启权限",
                  icon: "none"
                });
              }
            });
          } else if (res.cancel) {
          }
        }
      });
    },
    showShare() {
      this.show = true;
    },
    //生成海报
    generate() {
      this.show = false;
      this.poster_show.sharer_id;
      var params = {
        product_id: this.product_id,
        sharer_id: this.sharer_id
      };
      sharerLink(params, true)
        .then((res) => {
          if (res.code == 0) {
            let { product_qrcode, product_h5url, product_taglink } = res.data;
            this.img_src = product_qrcode.data.pic_url;
            this.imgshow = true;

            this.product_h5url = product_h5url.data;
            this.product_taglink = product_taglink.data;
          }
        })
        .catch(() => {
          this.$u.toast(this.$t("common.posterFail"));
          this.poster_show = false;
        });
    },

    // 点击预览图片
    previewImg() {
      uni.previewImage({
        current: "0",
        urls: [this.img_src]
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.share-section {
  width: 100%;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 121;
  .share-bg {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    .grey-bg {
      width: 100%;
      height: 63.48vh;
      background: #000000;
      opacity: 0.5;
    }
    .white-bg {
      flex: 1;
      background-color: #f2f2f2;
    }
  }
  .share-content {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 12.665vw;
    top: 0;
    z-index: 11;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    padding-bottom: 4.8vw;
    width: 74.67vw;
    .safe {
      padding-bottom: env(safe-area-inset-bottom);
    }
    .close-section {
      padding-top: 1vw;
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
      .iconfont {
        color: #ffffff;
        opacity: 0.3;
      }
      view {
        font-size: 26rpx;
        font-family: PingFang SC;
        font-weight: 600;
        color: #ffffff;
        margin-top: 1.067vw;
        opacity: 0.3;
      }
    }
    .to-friend {
      margin-top: 1.34vw;
      text-align: center;
      font-size: 40rpx;
      font-family: PingFang SC;
      font-weight: 600;
      color: #ffffff;
    }
    .poster-img {
      width: 74.67vw;
      height: 101.33vw;
      background: #ffffff;
      box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.05);
      opacity: 1;
      border-radius: 2.67vw;
      margin: 2.67vw auto 4vw;
      position: relative;
      overflow: hidden;
      .share-img {
        width: 100%;
        height: 100%;
        display: block;
      }
      .download-box {
        width: 8.8vw;
        height: 8.8vw;
        border-radius: 50%;
        background-color: rgba(0, 0, 0, 0.7);
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        right: 3.2vw;
        top: 3.73vw;
        z-index: 11;
        .iconfont {
          color: #ffffff;
          font-size: 44rpx;
        }
        .open-btn {
          width: 100%;
          height: 100%;
          opacity: 0;
          position: absolute;
          left: 0;
          top: 0;
          z-index: 12;
        }
      }
    }

    .poster-down-text {
      text-align: center;
      position: relative;
      font-size: 24rpx;
      font-family: PingFang SC;
      font-weight: 400;
      color: #8c8c8c;
    }

    .share-btn-group {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 4vw;
      .share-btn-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-right: 5.33vw;
        position: relative;
        .item-icon-box {
          width: 12.8vw;
          height: 12.8vw;
          border-radius: 50%;
          background-color: #ffffff;
          display: flex;
          justify-content: center;
          align-items: center;
          .iconfont {
            font-size: 50rpx;
          }

          .img {
            width: 40rpx;
          }
        }
        .item-text {
          font-size: 22rpx;
          font-family: PingFang SC;
          font-weight: 400;
          color: #262626;
          margin-top: 2.13vw;
        }
        .wxshareBtn {
          width: 100%;
          height: 100%;
          position: absolute;
          left: 0;
          top: 0;
          z-index: 11;
          opacity: 0;
        }
      }
      .share-btn-item:last-child {
        margin-right: 0;
      }
    }
  }
}
</style>
